package com.example.javaeekeshe.controller;

import com.example.javaeekeshe.pojo.AccountBean;
import com.example.javaeekeshe.pojo.FullOrderBean;
import com.example.javaeekeshe.pojo.GoodsBean;
import com.example.javaeekeshe.service.IAccountService;
import com.example.javaeekeshe.service.IGoodsService;
import com.example.javaeekeshe.service.IOrderService;
import com.example.javaeekeshe.utils.imageUtils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.nio.channels.Pipe;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAccountService accountService;
    @Autowired
    IOrderService orderService;
    @Autowired
    IGoodsService goodsService;

    @RequestMapping("/login/admin")
    public String login(String userName, String password, Model model, HttpSession session){
        AccountBean accountBean = accountService.existByAccount(userName, password);
        if (accountBean!=null&&Integer.parseInt(accountBean.getLeave())>=1){
            session.setAttribute("loginUser",accountBean);
            return "redirect:/admin/index";
        }
        else {
            System.out.println("密码错误");
            model.addAttribute("msg","不是管理员账户或账户密码不正确");
            return "/admin/login";
        }
    }

    //---------------------------------------------订单--------------------------
    /**
     * 订单管理界面
     * @param model
     * @return
     */
    @RequestMapping("/order/{ye}")
    public String order(@PathVariable int ye,Model model){
        List<FullOrderBean> orders = orderService.getAllOrder(ye);
        model.addAttribute("orders",orders);
        Integer rows = orderService.rowsAll();
        float sum= (float) (rows/5.0);
        if (sum>(int)sum){
            sum++;
        }
        model.addAttribute("sum",(int)sum);
        model.addAttribute("ye",ye);
        return "/admin/index/order";
    }

    @RequestMapping("/send/{id}/{ye}")
    public String send(@PathVariable("id") Integer id,@PathVariable Integer ye){
        orderService.send(id);
        return "redirect:/admin/order/"+ye;
    }

//----------------------------商品-----------------------------------
    /**
     * 跳转商品页面
     * @param model
     * @return
     */
    @RequestMapping("/toGood")
    public String toGood(Model model){
        List<GoodsBean> goodsBeans = goodsService.selectAllGood();
        model.addAttribute("goods",goodsBeans);
        return "/admin/index/goods";
    }

    /**
     * 上架
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/shelves/{id}")
    public String shelves(@PathVariable("id") Integer id,Model model){
        goodsService.seyState(id,1);
        goodsService.shelves(id);
        return "redirect:/admin/toGood";
    }

    /**
     * 下架
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/up/{id}")
    public String up(@PathVariable("id") Integer id,Model model){
        goodsService.seyState(id,0);
        goodsService.shelves(id);
        return "redirect:/admin/toGood";
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @RequestMapping("/delGood/{id}")
    public String delGood(@PathVariable("id") Integer id){
        goodsService.del(id);
        return "redirect:/admin/toGood";
    }

    /**
     * 编辑商品
     * @param good
     * @return
     */
    @RequestMapping("/updateGood")
    public String updateGood(GoodsBean good){
        System.out.println(good);
        goodsService.update(good);
        return "redirect:/admin/toGood";
    }

    @RequestMapping("/toadd")
    public String toAdd(){
        return "/admin/index/goodsAdd";
    }

    //图片上传接口
    @PostMapping("/upload")
    @ResponseBody
    public Map<String,Object> uploadImage(@RequestParam("file") MultipartFile file) {
        Map<String,Object> map  = new HashMap<>();
        String uploadDir = "D:\\Learn\\IDEAproject\\javaEEKeShe\\src\\main\\resources\\static\\img\\";
        try {
            // 图片路径
            String imgUrl = null;
            //上传
            String filename = UploadUtils.upload(file, uploadDir, file.getOriginalFilename());
            if (filename != null) {
                imgUrl = new File(uploadDir).getName() + "/" + filename;
            }
            map.put("code",0);
            map.put("msg","");
            map.put("data",filename);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","上传失败");
            map.put("data",null);
            return map;
        }
    }

    @RequestMapping("/addGoods")
    public String addGoods(GoodsBean goodsBean){
        goodsBean.setGstate("0");
        goodsService.addGoods(goodsBean);
        return "redirect:/admin/toGood";
    }
    //------------------------账户------------------------------

    @RequestMapping("/toAddAdmin")
    public String toAddAdmin(HttpSession session,Model model){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        if (loginUser.getLeave().equals("2")){
            return "/admin/index/addAdmin";
        }else {
            model.addAttribute("msg","你没有此权限");
            return "/admin/index/index";
        }
    }
    @RequestMapping("/addAdmin")
    public String addAdmin(AccountBean accountBean,HttpSession session,Model model){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        if (loginUser.getLeave().equals("2")){
            accountBean.setLeave("1");
            accountService.insertAdmin(accountBean);
            model.addAttribute("msg","添加成功");
        }
        else {
            model.addAttribute("msg","你没有此权限");
        }
        return "/admin/index/index";
    }
}
